package com.stillalive.springbootbooksystem.Controller;

import com.stillalive.springbootbooksystem.Model.UserInfo;
import com.stillalive.springbootbooksystem.Service.UserInfoService;
import com.stillalive.springbootbooksystem.constant.Constants;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

// 表现层
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/login")
    public boolean login(String userName,String password, HttpSession session){
        // 1.判断输入正确性
        if((!StringUtils.hasLength(userName)) || (!StringUtils.hasLength(password))){
            return false;
        }
        // 2.获取用户信息
        UserInfo userInfo = userInfoService.getUserInfoByName(userName);
        if(userInfo == null){
            return false;
        }
        // 3.检查账户密码
        if(password.equals(userInfo.getPassword())){
            session.setAttribute(Constants.SESSION_USER_KEY, userInfo);
            return true;
        }
        // 4.返回结果
        return false;
    }
}
